from pysqlite2 import dbapi2 as sqlite

db = sqlite.connect('database_final')

db.execute('drop table if exists secPagerank')
db.execute('create table secPagerank(urlid integer primary key,score double, norm_score double)')

for (urlid, rank,) in db.execute('select urlid, score from pagerank').fetchall():
	query = 'insert into secPagerank (urlid,score,norm_score) values(' + str(urlid) + ',' + str(rank) + ',0.0);'
	db.execute(query)

db.commit()

for (fromid, toid, ) in db.execute('select fromid, toid from link where rowid in (select linkid from cosSim where value>0.8)').fetchall():
	num_inlinks = db.execute('select count(*) from link where toid=%d' % toid).fetchone()[0]

	pr_to = db.execute('select score from secPagerank where urlid=%d' % toid).fetchone()[0]
	pr_from = db.execute('select score from secPagerank where urlid=%d' % fromid).fetchone()[0]

	pr_from = pr_from + pr_to/num_inlinks

	db.execute('update secPagerank set score=%f where urlid=%d' % (pr_from, fromid))

db.commit()
